﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyChungCu_V.BussinessTỉer
{
    class PeopleManagermentService
    {
        SQLConnection sql_conn;
        public PeopleManagermentService()
        {
            try
            {
                sql_conn = new SQLConnection(null); // Lay ConnectionString trong DataConnection.xml, neu file khong ton tai thi tao moi va ghi lai ConnectionString
            }
            catch (ApplicationException ex)
            {
                throw ex;
            }
        }
        


      
        public void AddPeople(string name, int age, DateTime date_of_birth)
        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;
            ctx.NguoiDans.InsertOnSubmit(new NguoiDan
            {
                TenND = name,
                TuoiND = age,
                NgaySinh = date_of_birth,
                GioiTinh = true,
                DanToc = " ",
                idTonGiao = Guid.NewGuid(),
                idNgheNghiep = Guid.NewGuid(),
                idNguoiDan = Guid.NewGuid(),
                SoDT = " ",
                SoCMT = " ",
            });
            ctx.SubmitChanges();
        }
      
       
       
        public IList<PeopleDTO> FindPeople(string name)
        {
            QLCCDataContext ctx = new QLCCDataContext();
            ctx.Connection.ConnectionString = sql_conn.ConnectionString;

            var people = from p in ctx.NguoiDans
                         where p.TenND.Contains(name)
                         select new PeopleDTO
                         {
                             id = p.idNguoiDan,
                             name = p.TenND,
                             age = (int)p.TuoiND,
                             date_of_birth = p.NgaySinh,
                             id_card_no = p.SoCMT,
                             id_job = p.idNgheNghiep,
                             id_race = p.idTonGiao,
                             id_religion = p.idTonGiao,
                             phone_num = p.SoDT,
                             race = p.DanToc,
                             religion = p.DanToc,
                             sex = p.GioiTinh
                         };
            return people.ToList<PeopleDTO>();
        }
   
        }
    }
